package com.slipper.system.dictionary.front;

import java.util.List;

import com.slipper.common.bo.Result;
import com.slipper.common.util.DicUtil;
import com.slipper.common.util.ResultUtil;
import com.slipper.system.dictionary.service.DictionaryDataService;
import com.slipper.system.dictionary.service.DictionaryService;

import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

import com.slipper.system.api.dictionary.bo.DictionaryBO;
import com.slipper.system.api.dictionary.bo.DictionaryDataBO;
import com.slipper.system.api.dictionary.bo.DictionaryDataBOs;
import com.slipper.system.api.dictionary.service.IDictionaryDataSv;

@RestController
public class DictionaryDataSv implements IDictionaryDataSv {
    @Resource
    private DictionaryService dictionaryService;
    @Resource
    private DictionaryDataService dictionaryDataService;
    
    @Override
    public Result<DictionaryDataBO> queryBy(DictionaryDataBO bo){
        return ResultUtil.successResult(dictionaryDataService.queryBy(bo.getDataId()));
    }
    
    @Override
    public Result<List<DictionaryDataBO>> queryPage(DictionaryDataBO bo) {
        return ResultUtil.successResult(this.dictionaryDataService.queryPage(bo));
    }
    
    @Override
    public Result<?> save(DictionaryDataBO bo){
        int result = dictionaryDataService.save(bo);
        DictionaryBO dictionary = this.dictionaryService.queryBy(bo.getDicId());
        DicUtil.clearCache(dictionary.getCode());
        return ResultUtil.saveResult(result);
    }
    
    @Override
    public Result<?> delete(DictionaryDataBO bo){
        if (bo.getDataId() != null) {
            DictionaryDataBO dataBO = this.dictionaryDataService.queryBy(bo.getDataId());
            DictionaryBO dictionary = this.dictionaryService.queryBy(dataBO.getDicId());
            DicUtil.clearCache(dictionary.getCode());
        }
        if (bo.getDataIds() != null) {
            for (String id : bo.getDataIds()) {
                DictionaryDataBO dataBO = this.dictionaryDataService.queryBy(id);
                DictionaryBO dictionary = this.dictionaryService.queryBy(dataBO.getDicId());
                DicUtil.clearCache(dictionary.getCode());
            }
        }
        
        return ResultUtil.deleteResult(dictionaryDataService.delete(bo));
    }

    @Override
    public Result<List<DictionaryDataBO>> queryList(DictionaryDataBO bo) {
        return ResultUtil.successResult(dictionaryDataService.queryList(bo));
    }

    @Override
    public Result<?> saves(DictionaryDataBOs bos) {
        int result = dictionaryDataService.saves(bos);
        DictionaryBO dictionary = this.dictionaryService.queryBy(bos.getDicId());
        DicUtil.clearCache(dictionary.getCode());
        return ResultUtil.saveResult(result);
    }

    @Override
    public Result<?> saveByCode(DictionaryDataBOs bos) {
        DictionaryBO bo = dictionaryService.queryByCode(bos.getDicCode());
        if (bo == null) {
            return ResultUtil.saveResult(0);
        }

        bos.setDicId(bo.getId());
        int result = dictionaryDataService.saveByCode(bos);
        DicUtil.clearCache(bos.getDicCode());
        return ResultUtil.saveResult(result);
    }
}
